<?php

namespace App\Jobs;

use App\Http\Controllers\EpsLngLatController;
use App\Models\EpsLngLat;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Facades\Log;

class TransferJob implements ShouldQueue
{
    use Queueable;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 5;

    /**
     * ins
     * @var EpsLngLat|null
     * create_at: 2025-09-09 09:12:04
     * update_at: 2025-09-09 09:12:04
     */
    public $eps = null;

    /**
     * Create a new job instance.
     * @param $data
     */
    public function __construct(public $data)
    {
        $this->onQueue(EpsLngLatController::TRANSFER_ON_QUEUE);
        $this->eps = $data;
    }

    /**
     * 中间件
     * @return array
     * create_at: 2025-01-11 09:49:56
     * update_at: 2025-01-11 09:49:56
     */
    public function middleware(): array
    {
        return [
//            new RateLimited4Trans,
            (new WithoutOverlapping($this->eps->ent_guid))->releaseAfter(1)
        ];
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            app(EpsLngLatController::class)->handleJobTransferJob($this->eps);
            $this->delete();
        } catch (Exception $e) {
            Log::error("ID 【{$this->eps->id}】： " . $e->getMessage());
        }
    }
}
